package com.zc.timedown.controller;


import com.alibaba.fastjson2.JSON;
import com.zc.common.core.controller.BaseController;
import com.zc.common.core.domain.AjaxResult;
import com.zc.common.core.domain.R;
import com.zc.common.core.domain.entity.SysDictData;
import com.zc.common.core.page.TableDataInfo;
import com.zc.common.enums.BusinessType;
import com.zc.common.utils.DateUtils;
import com.zc.common.utils.UserThreadLocal;
import com.zc.timedown.constants.AchievementTypeConstants;
import com.zc.timedown.dto.*;
import com.zc.timedown.domain.*;
import com.zc.timedown.mapper.ImportantLifeEventMapper;
import com.zc.timedown.service.*;
import com.zc.timedown.vo.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName:ApiWxController
 * Description:
 * DateTime: 2025/9/29 12:50
 * Author: zc
 * Version: 1.0
 */

@Api(tags = "小程序接口")
@RestController
@RequestMapping("/api/v1")
@Slf4j
public class ApiWxController extends BaseController {

    @Autowired
    private ApiUserService userService;

    @Autowired
    private ApiDailyQuoteService dailyQuoteService;

    @Autowired
    private ApiDailyReflectionService dailyReflectionService;

    @Autowired
    private ApiTodayPlanService todayPlanService;

    @Autowired
    private ApiTodayGoalService todayGoalService;

    @Autowired
    private ApiImportantLifeEventService apiImportantLifeEventService;

    @Autowired
    private ImportantLifeEventMapper importantLifeEventMapper;

    @Autowired
    private ApiTimeRecordService timeRecordService;

    @Autowired
    private ApiAchievementService apiAchievementService;

    @Autowired
    private ApiCategoryService categoryService;




    @PostMapping("/user/login")
    @ApiOperation("小程序登录")
    public R<LoginVo> login(@Valid @RequestBody UserLoginRequestDto userLoginRequestDto){
        LoginVo loginVo = userService.login(userLoginRequestDto);
        //测试
        return R.ok(loginVo);
    }

    @GetMapping("/user/info")
    @ApiOperation("获取用户信息")
    public R<UserInfoVo> info(){
        Long useId = UserThreadLocal.getUserId();
        User userInfo = userService.getUserInfo(useId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        AllStats allStats = todayPlanService.selectAllClockInStats(useId);
        userInfoVo.setClockInDays(allStats.getClockInDays());
        userInfoVo.setCompletionPercentage(allStats.getCompletionPercentage());
        return R.ok(userInfoVo);
    }

    @PostMapping("/user/info")
    @ApiOperation("修改用户信息")
    public R updateUserInfo( @RequestBody UserDto userDto){
        Long useId = UserThreadLocal.getUserId();
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        user.setId(useId);
        if (user.getId() == null){
           return R.fail("用户id不能为空");
        }
        if(user.getExpectedLifespan() != null && (user.getExpectedLifespan() < 60 || user.getExpectedLifespan() > 120)){
            return R.fail("期望寿命范围在60-120之间");
        }

        int row = userService.updateUserInfo(user);
        if (row > 0){
           return R.ok();
        }else {
            return R.fail();
        }
    }

    @GetMapping("/daily/quote")
    @ApiOperation("获取每日一句")
    public R<DailyQuote> getDailyQuote(@ApiParam(value = "每日一句ID", required = false)@RequestParam(value = "quoteId",required = false) Long quoteId ,
                                       @ApiParam(value = "获取类型（0固定，1随机）", required = true) @RequestParam(value = "sourceType") Integer sourceType) {
        // 调用服务获取每日一句
        DailyQuote dailyQuote = dailyQuoteService.getDailyQuote(quoteId,sourceType);
        return R.ok(dailyQuote);
    }

    @GetMapping("/daily/quoteList")
    @ApiOperation("获取自建每日一句列表")
    public R<List<DailyQuote>> getDailyQuoteList() {
        Long useId = UserThreadLocal.getUserId();
        List<DailyQuote> dailyQuotelist = dailyQuoteService.getDailyQuoteList(useId);
        return R.ok(dailyQuotelist);
    }


    @PostMapping("/daily/addQuote")
    @ApiOperation("添加每日一句")
    public R addDailyQuote(@RequestBody DailyQuoteDto dailyQuoteDto) {
        DailyQuote dailyQuote = new DailyQuote();
        BeanUtils.copyProperties(dailyQuoteDto,dailyQuote);
        Long userId = UserThreadLocal.getUserId();
        dailyQuote.setUserId(userId);
        dailyQuote.setIsSelfCreated(1);
        int row = dailyQuoteService.insertDailyQuote(dailyQuote);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/daily/updateQuote")
    @ApiOperation("修改每日一句")
    public R updateDailyQuote(@RequestBody DailyQuoteDto dailyQuoteDto) {
        DailyQuote dailyQuote = new DailyQuote();
        BeanUtils.copyProperties(dailyQuoteDto,dailyQuote);
        Long userId = UserThreadLocal.getUserId();
        dailyQuote.setUserId(userId);
        int row = dailyQuoteService.updateDailyQuote(dailyQuote);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/daily/delQuote/{quoteId}")
    @ApiOperation("删除每日一句")
    public R delDailyQuote(@PathVariable("quoteId") Long quoteId ) {
        int row = dailyQuoteService.deleteDailyQuoteById(quoteId);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


    @GetMapping("/daily/collectQuoteList")
    @ApiOperation("获取收藏每日一句列表")
    public R<List<DailyQuoteFavorite>> getDailyCollectQuote() {
        Long useId = UserThreadLocal.getUserId();
        List<DailyQuoteFavorite> dailyQuotelist = dailyQuoteService.collectDailyQuoteFavoriteList(useId);
        return R.ok(dailyQuotelist);
    }


    @PostMapping("/daily/collectQuote/{quoteId}")
    @ApiOperation("收藏每日一句")
    public R collectDailyQuote(@PathVariable("quoteId") Long quoteId) {
        Long useId = UserThreadLocal.getUserId();
        Integer row = dailyQuoteService.collectDailyQuote(useId, quoteId);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


    @PostMapping("/daily/cancelCollectQuote/{quoteId}")
    @ApiOperation("取消收藏每日一句")
    public R cancelCollectDailyQuote(@PathVariable("quoteId") Long quoteId) {
        Long useId = UserThreadLocal.getUserId();
        Integer row = dailyQuoteService.cancelCollectDailyQuote(useId, quoteId);
        return R.ok();
    }

    @PostMapping("/daily/addReflection")
    @ApiOperation("添加每日感悟")
    public R saveDailyReflection(@RequestBody DailyReflectionDto dailyReflectionDto) {
        Long useId = UserThreadLocal.getUserId();
        dailyReflectionDto.setUserId(useId);
        DailyReflection dailyReflection = new DailyReflection();
        BeanUtils.copyProperties(dailyReflectionDto,dailyReflection);
        dailyReflection.setTodayDate(LocalDate.now());
        dailyReflection.setCreateTime(DateUtils.getNowDate());
        int row = dailyReflectionService.insertDailyReflection(dailyReflection);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/daily/delReflection/{id}")
    @ApiOperation("删除每日感悟")
    public R delDailyReflection(@PathVariable("id") Long id) {
        Long useId = UserThreadLocal.getUserId();
        int row = dailyReflectionService.deleteDailyReflectionById(id);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/daily/updateReflection")
    @ApiOperation("修改每日感悟")
    public R updateDailyReflection(@RequestBody DailyReflectionDto dailyReflectionDto) {
        Long useId = UserThreadLocal.getUserId();
        dailyReflectionDto.setUserId(useId);
        DailyReflection dailyReflection = new DailyReflection();
        BeanUtils.copyProperties(dailyReflectionDto,dailyReflection);
        dailyReflection.setUpdateTime(DateUtils.getNowDate());
        boolean b = dailyReflectionService.updateById(dailyReflection);
        if (b){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @GetMapping("/daily/statisticsReflection")
    @ApiOperation("统计每日感悟心情数量")
    public R<DailyReflectionStatisticsVo> statisticsDailyReflection() {
        DailyReflectionStatisticsVo dailyReflectionStatisticsVo = dailyReflectionService.statisticsDailyReflection();
        return R.ok(dailyReflectionStatisticsVo);
    }


    @GetMapping("/daily/reflections")
    @ApiOperation("获取每日感悟列表")
    public R<List<DailyReflectionVo>> getDailyReflectionList() {
        Long useId = UserThreadLocal.getUserId();
        DailyReflection dailyReflection = new DailyReflection();
        dailyReflection.setUserId(useId);
        List<DailyReflection> dailyReflections = dailyReflectionService.selectDailyReflectionList(dailyReflection);
        List<DailyReflectionVo> dailyReflectionVos = dailyReflections.stream().map(dailyReflectionenEntity -> {
            DailyReflectionVo dailyReflectionVo = new DailyReflectionVo();
            BeanUtils.copyProperties(dailyReflectionenEntity, dailyReflectionVo);
            return dailyReflectionVo;
        }).collect(Collectors.toList());
        return R.ok(dailyReflectionVos);
    }

    @GetMapping("/daily/reflection")
    @ApiOperation("获取今日感悟")
    public R<DailyReflectionVo> getDailyReflection() {
        Long useId = UserThreadLocal.getUserId();
        DailyReflection dailyReflection = new DailyReflection();
        dailyReflection.setUserId(useId);
        dailyReflection.setTodayDate(LocalDate.now());
        List<DailyReflection> dailyReflections = dailyReflectionService.selectDailyReflectionList(dailyReflection);
        List<DailyReflectionVo> dailyReflectionVos = dailyReflections.stream().map(dailyReflectionenEntity -> {
            DailyReflectionVo dailyReflectionVo = new DailyReflectionVo();
            BeanUtils.copyProperties(dailyReflectionenEntity, dailyReflectionVo);
            return dailyReflectionVo;
        }).collect(Collectors.toList());

        if (dailyReflectionVos != null && dailyReflectionVos.size() > 0){
            return R.ok(dailyReflectionVos.get(0));
        }else {
            return R.ok();
        }
    }

    @GetMapping("/daily/plan")
    @ApiOperation("获取每日计划")
    public R<TodayPlanVo> getDailyPlan() {
        Long useId = UserThreadLocal.getUserId();
        TodayPlanVo dailyPlan = todayPlanService.getDailyPlan(useId);
        return R.ok(dailyPlan);
    }

    @PostMapping("/daily/planClock")
    @ApiOperation("打卡取消打卡")
    public R dailyPlan(@RequestBody TodayPlanItemDto todayPlanItemDto) {
        Long useId = UserThreadLocal.getUserId();
        todayPlanItemDto.setUserId(useId);
        todayPlanItemDto.setCompletedTime(LocalDateTime.now());
        int row = todayPlanService.updateTodayPlanItem(todayPlanItemDto);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/today/addGoal")
    @ApiOperation("添加今日目标")
    public R addTodayGoal(@RequestBody TodayGoalDto todayGoalDto) {
        Long useId = UserThreadLocal.getUserId();
        todayGoalDto.setUserId(useId);
        TodayGoal todayGoal = new TodayGoal();
        BeanUtils.copyProperties(todayGoalDto,todayGoal);
        todayGoal.setIsEnabled(1);
        int row = todayPlanService.insertTodayGoalAndPlanItem(todayGoal);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/today/updateGoal")
    @ApiOperation("修改今日目标")
    public R updateTodayGoal(@RequestBody TodayGoalDto todayGoalDto) {
        Long useId = UserThreadLocal.getUserId();
        todayGoalDto.setUserId(useId);
        TodayGoal todayGoal = new TodayGoal();
        BeanUtils.copyProperties(todayGoalDto,todayGoal);
        int row = todayGoalService.updateTodayGoal(todayGoal);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }

    }

    @PostMapping("/today/delGoal")
    @ApiOperation("删除今日目标")
    public R delTodayGoal(@RequestBody TodayPlanItemDto todayPlanItemDto) {
        int row = todayPlanService.delTodayGoalAndPlanItem(todayPlanItemDto);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


    @GetMapping("/life/events")
    @ApiOperation("获取人生重要节点列表")
    public R<List<ImportantLifeEventVo>> getLifeEventList() {
        Long useId = UserThreadLocal.getUserId();
        ImportantLifeEvent importantLifeEvent = new ImportantLifeEvent();
        importantLifeEvent.setUserId(useId);
        List<ImportantLifeEvent> importantLifeEvents = apiImportantLifeEventService.selectImportantLifeEventList(importantLifeEvent);
        List<ImportantLifeEventVo> importantLifeEventVos = importantLifeEvents.stream().map(event -> {
            ImportantLifeEventVo importantLifeEventVo = new ImportantLifeEventVo();
            BeanUtils.copyProperties(event, importantLifeEventVo);
            return importantLifeEventVo;
        }).collect(Collectors.toList());
        return R.ok(importantLifeEventVos);
    }


    @PostMapping("/life/addEvent")
    @ApiOperation("添加人生重要节点")
    public R addLifeEvent(@RequestBody ImportantLifeEventDto importantLifeEventDto) {
        Long useId = UserThreadLocal.getUserId();
        importantLifeEventDto.setUserId(useId);
        ImportantLifeEvent importantLifeEvent = new ImportantLifeEvent();
        BeanUtils.copyProperties(importantLifeEventDto,importantLifeEvent);
        int row = importantLifeEventMapper.insertImportantLifeEvent(importantLifeEvent);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/life/editEvent")
    @ApiOperation("编辑人生重要节点")
    public R editLifeEvent(@RequestBody ImportantLifeEventDto importantLifeEventDto) {
        Long useId = UserThreadLocal.getUserId();
        importantLifeEventDto.setUserId(useId);
        ImportantLifeEvent importantLifeEvent = new ImportantLifeEvent();
        BeanUtils.copyProperties(importantLifeEventDto,importantLifeEvent);
        int row = importantLifeEventMapper.updateImportantLifeEvent(importantLifeEvent);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @PostMapping("/life/delEvent/{id}")
    @ApiOperation("删除人生重要节点")
    public R editLifeEvent(@PathVariable("id") Long id) {
        Long useId = UserThreadLocal.getUserId();
        int row = importantLifeEventMapper.deleteImportantLifeEventById(id);
        if (row > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }


    @GetMapping("/daily/plan/weekly-statistics")
    @ApiOperation("获取最近今日目标7天计划完成统计")
    public R<List<WeeklyPlanStatisticsVo>> getWeeklyPlanStatistics() {
        Long userId = UserThreadLocal.getUserId();
        List<WeeklyPlanStatisticsVo> statistics = todayPlanService.getWeeklyPlanStatistics(userId);
        return R.ok(statistics);
    }

    @GetMapping("/daily/plan/dailyStatistics")
    @ApiOperation("获取指定日期范围内每日计划按天统计统计")
    public R<List<DailyPlanStatisticsVo>> getDailyPlanStatistics(
            @ApiParam(value = "开始日期(yyyy-MM-dd)", required = true) @RequestParam("startDate") String startDate,
            @ApiParam(value = "结束日期(yyyy-MM-dd)", required = true) @RequestParam("endDate") String endDate) {
        Long userId = UserThreadLocal.getUserId();
        List<DailyPlanStatisticsVo> statistics = todayPlanService.getDailyPlanStatistics(userId, startDate, endDate);
        return R.ok(statistics);
    }




    @GetMapping("/daily/plan/monthlyCompleted")
    @ApiOperation("获取今日目标最近30天每天完成计划统计")
    public R getMonthlyCompletedStatistics() {
        Long userId = UserThreadLocal.getUserId();
        List<DailyCompletedStatisticsVo> statistics = todayPlanService.getMonthlyCompletedStatistics(userId);
        return R.ok(statistics);
    }

    /**
     * 获取最近4周的计划统计数据
     */
    @GetMapping("/daily/plan/fourWeeklyStatistics")
    @ApiOperation("获取今日目标最近4周周计划统计")
    public R<List<WeeklyStatsVO>> getRecent4WeeksStats() {
        List<WeeklyStatsVO> stats = todayPlanService.getRecent4WeeksStats();
        return R.ok(stats);
    }

    @GetMapping("/daily/plan/timeRangeStatistics")
    @ApiOperation("获取指定日期范围内今日目标计划完成统计")
    public R<TimeRangeStatisticsVo> getTimeRangeStatistics(
            @ApiParam(value = "开始日期(yyyy-MM-dd)", required = true) @RequestParam("startDate") String startDate,
            @ApiParam(value = "结束日期(yyyy-MM-dd)", required = true) @RequestParam("endDate") String endDate) {
        Long userId = UserThreadLocal.getUserId();
        TimeRangeStatisticsVo timeRangeStatisticsVo = todayPlanService.getTimeRangeStats(userId, startDate, endDate);
        return R.ok(timeRangeStatisticsVo);
    }


    @GetMapping("/daily/plan/monthlyStatistics")
    @ApiOperation("获取今日目标指定月份的月统计")
    public R<List<MonthlyStatsVo>> getMonthlyClockInStats(
            @ApiParam(value = "年份", required = true) @RequestParam("year") Integer year,
            @ApiParam(value = "月份", required = true) @RequestParam("month") Integer month) {
        Long userId = UserThreadLocal.getUserId();
        List<MonthlyStatsVo> stats = todayPlanService.getMonthlyClockInStats(userId, year, month);
        return R.ok(stats);
    }


    @GetMapping("/life/lifeSpanStatistics")
    @ApiOperation("获取人生进度统计")
    public R<LifeSpanStatisticsVo> getLifeSpanStatistics() {
        Long userId = UserThreadLocal.getUserId();
        LifeSpanStatisticsVo lifeSpanStatistics = userService.getLifeSpanStatistics(userId);
        return R.ok(lifeSpanStatistics);
    }


    /**
     * 新增时间账本
     */

    @PostMapping("/time/record")
    @ApiOperation("新增时间账本")
    public R add(@RequestBody TimeRecordDTO timeRecordDTO)
    {
        Long userId = UserThreadLocal.getUserId();
        timeRecordDTO.setUserId(userId);
        TimeRecord timeRecord = new TimeRecord();
        BeanUtils.copyProperties(timeRecordDTO,timeRecord);

        int row = timeRecordService.insertTimeRecord(timeRecord);
        if (row <= 0){
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/time/record/{id}")
    @ApiOperation("获取时间账本详情")
    public R<TimeRecord> getTimeRecordDetail(@PathVariable("id") Long id){
        TimeRecord timeRecord = timeRecordService.selectTimeRecordById(id);
        return R.ok(timeRecord);
    }

    @GetMapping("/time/records/daily")
    @ApiOperation("查询用户某一天的时间账本列表")
    public R<List<TimeRecord>> getTimeRecordsByDate(
            @ApiParam(value = "年份", required = true) @RequestParam("year") Integer year,
            @ApiParam(value = "月份", required = true) @RequestParam("month") Integer month,
            @ApiParam(value = "日期", required = true) @RequestParam("day") Integer day) {
        Long userId = UserThreadLocal.getUserId();
        LocalDate date = LocalDate.of(year, month, day);
        List<TimeRecord> timeRecords = timeRecordService.getTimeRecordsByDate(userId, date);
        return R.ok(timeRecords);
    }


    @GetMapping("/time/statistics/monthly")
    @ApiOperation("获取月度时间账本统计")
    public R<List<TimeRecordDailyStatisticsVo>> getMonthlyTimeStatistics(
            @ApiParam(value = "年份", required = true) @RequestParam("year")Integer year,
            @ApiParam(value = "月份", required = true) @RequestParam("month") Integer month) {
        Long userId = UserThreadLocal.getUserId();
        List<TimeRecordDailyStatisticsVo> statistics = timeRecordService.getMonthlyTimeStatistics(userId, year, month);
        return R.ok(statistics);
    }

    @GetMapping("/time/statistics/weeklyCategory")
    @ApiOperation("获取本周时间账本统计")
    public R<TimeRecordWeeklyStatisticsVo> getWeeklyTimeStatisticsByCategory() {
        Long userId = UserThreadLocal.getUserId();
        TimeRecordWeeklyStatisticsVo timeRecordWeeklyStatisticsVo = timeRecordService.getWeeklyTimeStatisticsByCategory(userId);
        return R.ok(timeRecordWeeklyStatisticsVo);
    }

    @GetMapping("/time/statistics/monthlyCategory")
    @ApiOperation("获取时间账本年度每月分类时间统计")
    public R<TimeRecordMonthlyCategoryStatisticsVo> getMonthlyCategoryStatistics(
            @ApiParam(value = "年份", required = true) @RequestParam("year") Integer year,
            @ApiParam(value = "月份", required = true) @RequestParam("month") Integer month) {
        Long userId = UserThreadLocal.getUserId();
        TimeRecordMonthlyCategoryStatisticsVo statistics = timeRecordService.getMonthlyCategoryStatistics(userId, year,month);
        return R.ok(statistics);
    }

    @GetMapping("/time/statistics/daily")
    @ApiOperation("获取指定日期的时间账本统计")
    public R<TimeRecordDailyStatisticsVo> getDailyTimeStatistics(
            @ApiParam(value = "年份", required = true) @RequestParam("year") Integer year,
            @ApiParam(value = "月份", required = true) @RequestParam("month") Integer month,
            @ApiParam(value = "日期", required = true) @RequestParam("day") Integer day) {
        Long userId = UserThreadLocal.getUserId();
        LocalDate date = LocalDate.of(year, month, day);
        TimeRecordDailyStatisticsVo statistics = timeRecordService.getDailyTimeStatisticsByDate(userId, date);
        return R.ok(statistics);
    }

    /**
     * 获取用户成就列表
     */
    @GetMapping("/user/achievements")
    @ApiOperation("获取用户成就列表")
    public R<List<AchievementVo>> getUserAchievements() {
        Long userId = UserThreadLocal.getUserId();
        List<AchievementVo> achievements = apiAchievementService.getUserAchievements(userId);
        return R.ok(achievements);
    }


    @GetMapping("/test/punchcard/{id}")
    @ApiOperation("测试接口插入用户成就数据")
    public R getPunchCard(@PathVariable(value = "id") Long id) {
//        Long userId = UserThreadLocal.getUserId();
        Achievement achievement = new Achievement();
        achievement.setUserId(id);
        achievement.setAchievedDate(LocalDateTime.now());
        achievement.setAchievementType(AchievementTypeConstants.CONSECUTIVE_3_DAYS);
        int row = apiAchievementService.insertAchievement(achievement);
        if (row <= 0){
            return R.fail();
        }
        return R.ok();
    }

    @GetMapping("/category/list")
    @ApiOperation("获取用户分类列表")
    public TableDataInfo getCategoryList(@ApiParam(value = "字典类型 时间账本：time_ledger_category" +
            " 今日目标：today_goal_category" +
            " 重要节点分类：important_node_category", required = true)
                                             @RequestParam("dictType") String dictType,
                                         @ApiParam(value = "页码", required = false)
                                         @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                         @ApiParam(value = "每页条数", required = false)
                                             @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Long userId = UserThreadLocal.getUserId();
        startPage();
        List<SysDictData> list = categoryService.selectCategoryList(dictType,userId);
        return getDataTable(list);
    }
    /**
     * 新增分类
     * 创建用户自定义分类，系统自动设置创建人和默认状态
     *
     * @param category 分类信息
     * @return 操作结果
     */
    @ApiOperation("新增分类")
    @PostMapping("/category/add")
    public AjaxResult add(@Validated @RequestBody SysDictData category) {
        Long userId = UserThreadLocal.getUserId();
        category.setCreateBy(String.valueOf(userId));
        category.setIsDefault("N");
        return toAjax(categoryService.insertCategory(category));
    }

    /**
     * 修改分类
     * 更新用户自定义分类信息
     *
     * @param category 分类信息
     * @return 操作结果
     */
    @ApiOperation("修改分类")
    @PostMapping("/category/edit")
    public AjaxResult edit(@Validated @RequestBody SysDictData category) {
        log.info("------------------修改分类--------------------------");
        log.info(JSON.toJSONString(category));
        log.info("--------------------------------------------");
        Long userId = UserThreadLocal.getUserId();
        category.setUpdateBy(String.valueOf(userId));
        return toAjax(categoryService.updateCategory(category, userId));
    }

    /**
     * 删除分类
     * 批量删除用户自定义分类，默认分类不可删除
     *
     * @param dictCodes 分类编码数组
     * @return 操作结果
     */
    @ApiOperation("删除分类")
    @DeleteMapping("/category/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        Long userId = UserThreadLocal.getUserId();
        return toAjax(categoryService.deleteCategoryByIds(dictCodes, userId));
    }


}
